<?php

//power by wshoto.com
if (!defined('IN_IA')) {
	die('Access Denied');
}
global $_W, $_GPC;
function sortByTime($a, $b)
{
	if ($a['ts'] == $b['ts']) {
		return 0;
	} else {
		return $a['ts'] > $b['ts'] ? 1 : -1;
	}
}


//物流补丁 20161117
function getList($express, $expresssn){
	if($express == 'guangdongyouzhengwuliu'){
		$express = 'ems';
	}
	$url = "http://www.kuaidi100.com/query?type={$express}&postid={$expresssn}&id=1&valicode=&temp=0.19689508604579842";
	$resp = file_get_contents($url);
	$resp = json_decode($resp,1);
	return $resp['data'];
}


function getList1($express, $expresssn)
{

	if($express == 'guotongkuaidi'){
		$uri = 'http://222.73.113.82/taobao_query/tb_jiekou.aspx?mailno='.$expresssn;

		$resp = file_get_contents($uri);
		$resp = iconv('gbk', 'utf8', $resp);
		$resp = json_decode($resp);

		return $resp->traces;
	}

	if($express == 'ems'){
		$uri = 'http://211.156.193.140:8000/cotrackapi/api/track/mail/'.$expresssn;
		load()->func('communication');
		$resp = ihttp_request($uri, '', array('version' => 'ems_track_cn_1.0', 'authenticate' => 'szxhbtx_12kledu90sau'));
		//$resp = iconv('gbk', 'utf8', $resp);
		$resp = json_decode($resp['content']);

		return $resp->traces;
	}

	return false;
}
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$openid = m('user')->getOpenid();
$uniacid = $_W['uniacid'];
$orderid = intval($_GPC['id']);
if ($_W['isajax']) {
	if ($operation == 'display') {
		$order = pdo_fetch('select * from ' . tablename('ewei_shop_order') . ' where id=:id and uniacid=:uniacid and openid=:openid limit 1', array(':id' => $orderid, ':uniacid' => $uniacid, ':openid' => $openid));
		if (empty($order)) {
			show_json(0);
		}
		$goods = pdo_fetchall("select og.goodsid,og.price,g.title,g.thumb,og.total,g.credit,og.optionid,og.optionname as optiontitle,g.isverify,g.storeids  from " . tablename('ewei_shop_order_goods') . " og " . " left join " . tablename('ewei_shop_goods') . " g on g.id=og.goodsid " . " where og.orderid=:orderid and og.uniacid=:uniacid ", array(':uniacid' => $uniacid, ':orderid' => $orderid));
		$goods = set_medias($goods, 'thumb');
		$order['goodstotal'] = count($goods);
		$set = set_medias(m('common')->getSysset('shop'), 'logo');
		show_json(1, array('order' => $order, 'goods' => $goods, 'set' => $set));
	} else {
		if ($operation == 'step') {
			$express = trim($_GPC['express']);
			$expresssn = trim($_GPC['expresssn']);

			//物流补丁20161117
			$arr = getList($express, $expresssn);
			if (!$arr) {
				$arr = getList($express, $expresssn);
				if (!$arr) {
					show_json(1, array('list' => array()));
				}
			}
			show_json(1, array('list' => $arr));


//			$expressInfo = getList($express, $expresssn);
//
//			$expressInfo = (array)$expressInfo;
//			$expressList = array();
//
//			foreach($expressInfo as $key => $itemx){
//				$itemx = (array) $itemx;
//				if($express == 'ems'){
//					$expressList[] = array('time' => $itemx['acceptTime'], 'step' => $itemx['acceptAddress']. ' '. $itemx['remark']);
//				}else{
//					$expressList[] = array('time' => $itemx['acceptTime'], 'step' => $itemx['acceptAddress']);
//				}
//			}

			/*$arr = getList($express, $expresssn);
			if (!$arr) {
				$arr = getList($express, $expresssn);
				if (!$arr) {
					show_json(1, array('list' => array()));
				}
			}
			$len = count($arr);
			$step1 = explode("<br />", str_replace("&middot;", "", $arr[0]));
			$step2 = explode("<br />", str_replace("&middot;", "", $arr[$len - 1]));
			for ($i = 0; $i < $len; $i++) {
				if (strtotime(trim($step1[0])) > strtotime(trim($step2[0]))) {
					$row = $arr[$i];
				} else {
					$row = $arr[$len - $i - 1];
				}
				$step = explode("<br />", str_replace("&middot;", "", $row));
				$list[] = array('time' => trim($step[0]), 'step' => trim($step[1]), 'ts' => strtotime(trim($step[0])));
			}*/
//			show_json(1, array('list' => $expressList));
		}
	}
}
include $this->template('order/express');